package org.xukai.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
import org.xukai.common.ComparableStudent;

import javax.xml.datatype.XMLGregorianCalendar;
import java.lang.reflect.Type;

/**
 * @author xukai
 * @desc
 * @date 2017-06-05-10:43
 */
public  class OrderDeserializer<T> implements ObjectDeserializer {

    static String s = "{\"amazonOrderId\":\"112-2691233-8689825\",\"buyerEmail\":\"jjjb1wpcb2k2t4l@marketplace.amazon.com\",\"buyerName\":\"eileen\",\"earliestDeliveryDate\":{\"day\":9,\"eonAndYear\":2017,\"hour\":7,\"millisecond\":-2147483648,\"minute\":0,\"month\":6,\"second\":0,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"localPart\":\"dateTime\",\"namespaceURI\":\"http://www.w3.org/2001/XMLSchema\",\"prefix\":\"\"},\"year\":2017},\"earliestShipDate\":{\"day\":19,\"eonAndYear\":2017,\"hour\":7,\"millisecond\":-2147483648,\"minute\":0,\"month\":5,\"second\":0,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"$ref\":\"$.earliestDeliveryDate.xMLSchemaType\"},\"year\":2017},\"fulfillmentChannel\":\"MFN\",\"isBusinessOrder\":false,\"isPremiumOrder\":false,\"isPrime\":false,\"isReplacementOrder\":false,\"lastUpdateDate\":{\"day\":19,\"eonAndYear\":2017,\"hour\":9,\"millisecond\":-2147483648,\"minute\":56,\"month\":5,\"second\":29,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"$ref\":\"$.earliestDeliveryDate.xMLSchemaType\"},\"year\":2017},\"latestDeliveryDate\":{\"day\":1,\"eonAndYear\":2017,\"hour\":6,\"millisecond\":-2147483648,\"minute\":59,\"month\":7,\"second\":59,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"$ref\":\"$.earliestDeliveryDate.xMLSchemaType\"},\"year\":2017},\"latestShipDate\":{\"day\":23,\"eonAndYear\":2017,\"hour\":6,\"millisecond\":-2147483648,\"minute\":59,\"month\":5,\"second\":59,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"$ref\":\"$.earliestDeliveryDate.xMLSchemaType\"},\"year\":2017},\"marketplaceId\":\"ATVPDKIKX0DER\",\"numberOfItemsShipped\":1,\"numberOfItemsUnshipped\":0,\"orderStatus\":\"Shipped\",\"orderTotal\":{\"amount\":\"6.00\",\"currencyCode\":\"USD\",\"setAmount\":true,\"setCurrencyCode\":true},\"orderType\":\"StandardOrder\",\"paymentExecutionDetail\":[],\"paymentMethod\":\"Other\",\"paymentMethodDetails\":[\"CreditCard\"],\"purchaseDate\":{\"day\":18,\"eonAndYear\":2017,\"hour\":11,\"millisecond\":-2147483648,\"minute\":33,\"month\":5,\"second\":27,\"timezone\":0,\"valid\":true,\"xMLSchemaType\":{\"$ref\":\"$.earliestDeliveryDate.xMLSchemaType\"},\"year\":2017},\"salesChannel\":\"Amazon.com\",\"setAmazonOrderId\":true,\"setBuyerCounty\":false,\"setBuyerEmail\":true,\"setBuyerName\":true,\"setBuyerTaxInfo\":false,\"setCbaDisplayableShippingLabel\":false,\"setEarliestDeliveryDate\":true,\"setEarliestShipDate\":true,\"setFulfillmentChannel\":true,\"setIsBusinessOrder\":true,\"setIsPremiumOrder\":true,\"setIsPrime\":true,\"setIsReplacementOrder\":true,\"setLastUpdateDate\":true,\"setLatestDeliveryDate\":true,\"setLatestShipDate\":true,\"setMarketplaceId\":true,\"setNumberOfItemsShipped\":true,\"setNumberOfItemsUnshipped\":true,\"setOrderChannel\":false,\"setOrderStatus\":true,\"setOrderTotal\":true,\"setOrderType\":true,\"setPaymentExecutionDetail\":false,\"setPaymentMethod\":true,\"setPaymentMethodDetails\":true,\"setPurchaseDate\":true,\"setPurchaseOrderNumber\":false,\"setReplacedOrderId\":false,\"setSalesChannel\":true,\"setSellerOrderId\":false,\"setShipServiceLevel\":true,\"setShipmentServiceLevelCategory\":true,\"setShippedByAmazonTFM\":true,\"setShippingAddress\":true,\"setTFMShipmentStatus\":false,\"shipServiceLevel\":\"Std US D2D Dom\",\"shipmentServiceLevelCategory\":\"Standard\",\"shippedByAmazonTFM\":false,\"shippingAddress\":{\"addressLine1\":\"2247 JULIESSE AVE\",\"city\":\"SACRAMENTO\",\"countryCode\":\"US\",\"name\":\"Bella\",\"phone\":\"95821-4628\",\"postalCode\":\"95821-4628\",\"setAddressLine1\":true,\"setAddressLine2\":false,\"setAddressLine3\":false,\"setCity\":true,\"setCountryCode\":true,\"setCounty\":false,\"setDistrict\":false,\"setName\":true,\"setPhone\":true,\"setPostalCode\":true,\"setStateOrRegion\":true,\"stateOrRegion\":\"CA\"}}";

    @Override
    public <T> T deserialze(DefaultJSONParser defaultJSONParser, Type type, Object o) {
        JSONLexer lexer = defaultJSONParser.getLexer();
//        for (int i = 0; i < 100; i++) {
//            System.out.println(lexer.token() + "_" + i);
//            lexer.nextToken();
//        }
        lexer.nextToken(JSONToken.RBRACE);
//        System.out.println(lexer.token());
//                for (int i = 0; i < 10; i++) {
//            System.out.println(lexer.token() + "_" + i);
//            lexer.nextToken();
//        }
        lexer.nextToken(JSONToken.RBRACE);
        lexer.nextToken(16);
        lexer.nextToken(16);
        JSONObject obj = (JSONObject) JSON.parseObject(defaultJSONParser.getInput()).get((String) o);
        int timezone = obj.getIntValue("timezone");
        int year = obj.getIntValue("year");
        int month = obj.getIntValue("month");
        int day = obj.getIntValue("day");
        int minute = obj.getIntValue("minute");
        int second = obj.getIntValue("second");
        XMLGregorianCalendarImpl xmlGregorianCalendar = new XMLGregorianCalendarImpl();
        xmlGregorianCalendar.setTimezone(timezone);
        xmlGregorianCalendar.setYear(year);
        xmlGregorianCalendar.setMonth(month);
        xmlGregorianCalendar.setDay(day);
        xmlGregorianCalendar.setHour(day);
        xmlGregorianCalendar.setMinute(minute);
        xmlGregorianCalendar.setSecond(second);
        return (T)xmlGregorianCalendar;
    }

    @Override
    public int getFastMatchToken() {
        return 13;
    }


    public static void main(String[] args) {
        ParserConfig.getGlobalInstance().putDeserializer(XMLGregorianCalendar.class, new OrderDeserializer<XMLGregorianCalendar>());
        ComparableStudent order = JSON.parseObject(s, ComparableStudent.class);
        System.out.println(order);
    }


}
